package com.yt.lxh.acl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.lxh.acl.mapper.RoleMenuMapper;
import com.yt.lxh.acl.service.MenuService;
import com.yt.lxh.acl.service.RoleMenuService;
import com.yt.lxh.common.service.CheckLogin;
import com.yt.lxh.dto.menu.AssignMenuDto;
import com.yt.lxh.model.acl.Menu;
import com.yt.lxh.model.acl.RoleMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private MenuService menuService;
    @Autowired
    private CheckLogin checkLogin;
    //1 查询所有菜单 和 查询角色分配过菜单id列表
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Integer roleId) {
        // 查询所有菜单
        List<Menu> menuList = menuService.findNodes();
        // 查询角色分配过菜单id列表
        List<Integer> menuIdListByRoleId = roleMenuMapper.findRoleMenuByRoleId(roleId);
        Map<String,Object> map = new HashMap<>();
        map.put("menuList",menuList);
        map.put("menuIdListByRoleId",menuIdListByRoleId);
        return map;
    }

    //分配菜单
    @Override
    public void doAssign(AssignMenuDto assignMenuDto) {
        //删除角色之前分配过菜单数据
        roleMenuMapper.deleteById(assignMenuDto.getRoleId());
        //保存分配数据
        List<Map<String, Number>> menuInfo = assignMenuDto.getMenuIdList();
        //角色分配了菜单
        if (menuInfo != null && menuInfo.size() > 0) {
            roleMenuMapper.doAssign(assignMenuDto);
        }
    }
}
